from fastapi import APIRouter, Depends
from pydantic import BaseModel

from backend.app.app.api.api_v1.user import verify_token_user
from backend.app.app.crud_pro.tb_page_BP import get_page_info,add_data, get_page_data, add_page_data

page_BP_router = APIRouter(prefix="/page_BP", tags=["3.单元方案措施效果预测---压裂-BP神经网络模型训练-页面数据"])

class u_p_id(BaseModel):
    user_id : str
    proj_id : str


@page_BP_router.post("/read_page", name="读取页面信息")
async def read_page(data:u_p_id,verify=Depends(verify_token_user)):
    info = get_page_info(user_id=data.user_id,pro_id=data.proj_id)
    return {"page_infomation":info,"verify":verify}

class save_data(BaseModel):
    user_id : str
    proj_id : str
    table_data: str
    select_model: str
    select_f_name: str
    select_d_name: str
    start_year: str

@page_BP_router.post("/save_page", name="存储页面信息")
async def save_page_(page_data:save_data, verify=Depends(verify_token_user)):
    res = add_data(dict(page_data))
    if not res['res']:
        return {'result': res['res'], 'error': res['error'], "verify":verify}
    return {'result': res['res'], 'verify': verify}


class read_page_data(BaseModel):
    user_id: str
    proj_id: str

@page_BP_router.post("/read_page_data", name="读取页面数据")
async def read_page_data(data: read_page_data, verify=Depends(verify_token_user)):
    data = get_page_data(user_id=data.user_id, pro_id=data.proj_id)
    return {'page_data': data, 'verify': verify}



class save_page_data(BaseModel):
    user_id: str
    proj_id: str
    model_name: str
    unit_name: str
    start_end_date: str

@page_BP_router.post("/save_page_data", name="存储页面数据")
async def save_page_data(save_data: save_page_data, verify=Depends(verify_token_user)):
    res = add_page_data(dict(save_data))
    if not res['res']:
        return {'result': res['res'], 'error': res['error']}
    return {'result': res['res'], 'verify': verify}